if(!dojo._hasResource["opm.widgets.GenericPage"]){
dojo._hasResource["opm.widgets.GenericPage"] = true;
dojo.provide("opm.widgets.GenericPage");
dojo.require("dijit._Widget");
dojo.require("opm.widgets.GenericForm");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.Toolbar");
dojo.declare("opm.widgets.GenericPage",dijit._Widget,
{
   contentId:"",
   widgetId:"",
   childWidgetId:"",
   pageUI:[],
   dataPage:{},
   ajaxCallUrl:"",
   ajaxCallType:"",
   currentForm:null,
   currentFormWidgetId:null,
   selectedData:null,
   selectedIndex:0,

   contructCurrentForm:null,
   _handleSubscribeEvent:{},
   _handleEvents:{},
   existedforms:[],
   postCreated:null,
   constructor:function(args){
      this.contentId = args.contentId;
      this.widgetId = args.id;
   }, 
   setDataPage:function(args){
      var _args = (args==null)?{}:args;
      this.pageUI = (_args.pageUI==null)?[]:_args.pageUI;
      this.dataPage = args;
      this.existedforms = [];
   },
   setAtt:function(attName, value){
      this[attName] = value;
   },
   getAtt:function(attName){
      return this.dataPage[attName];
   },
   empty:function (){
      dijit.registry.remove(this.contentId);
   },
   createPage:function(){
      var commonData = this;
      var args = {
         ajaxUrl: this.ajaxCallUrl,
         callback: function (data){
            commonData.setDataPage(data);
            commonData.generic();
            if (commonData.postCreated != null){
               commonData.postCreated();
            }
       }};
      OPM.callAjaxWidget(args, this.contentId, this.ajaxCallType);
   },
   generic:function(){
      this.empty();
      this.selectedData = null;
      var formIdToInserContent = "";
      for (var index=0; index<this.pageUI.length; index++){
         var layoutContainer = this.pageUI[index];
         var layoutType = (layoutContainer.type==null)?"":layoutContainer.type;
         switch (layoutType){
            case "tab":
               this._genericTab(layoutContainer);
               formIdToInserContent = dijit.byId(this.childWidgetId).getChildren()[this.selectedIndex].id;
               break;
            case "simpleTab":
               this._genericSimpleTab(layoutContainer);
               formIdToInserContent = this.childWidgetId;
               break;
            case "panel":
               this._genericPanel(layoutContainer, index);
               break;
            case "toolbar":
               this._genericToolbar(layoutContainer, index);
               break;
         }
      }
      if (this.selectedData != null){
         var genericForm = this.contructCurrentForm;
         if (genericForm == null){
            var formData = {
               dataform: this.selectedData,
               contentId:formIdToInserContent,
               widgetId:this.currentFormWidgetId
            },
            genericForm = OPM.genericFormWidget(formData);
         }else {
            genericForm = this.contructCurrentForm(this.selectedData, formIdToInserContent);
         }
         genericForm.setAtt("contentId", formIdToInserContent);
         genericForm.show();
         this.currentForm = genericForm;
      }
   },
   connectEvent:function(name, callBackFunction){
      var eventName = this.widgetId + "_" + name;
      if (this._handleSubscribeEvent[eventName] == null){
         console.log('Subscribe: ' + eventName);
         this._handleSubscribeEvent[eventName] = dojo.subscribe(eventName, function(data){
            callBackFunction(data);
         });
      }
   },
   handleEvent:function(eventName, callBackFunction){
       this._handleEvents[eventName] = callBackFunction;
   },
   callEvent:function(eventName){
       this._handleEvents[eventName]();
   },
   callPostText:function(dataform){
      var ajaxUrl = dataform.ajaxUrl;
      OPM.ajaxWidget({
         ajaxUrl: ajaxUrl,
         callback: function (data){
            dataform.handleSuccess(data);
         }
      },this.contentId).callXhrPostText();
   },
   _genericTab:function(tabContainer){
      dojo.byId(this.contentId).innerHTML = '<div id="insertContentId" style="width: 100%; height: 100%;"></div>';
      var tabPosition = (tabContainer.tabPosition==null)?'':'tabPosition="'+tabContainer.tabPosition+'"' ;
      var target = new dijit.layout.TabContainer({
         style:"width: 100%; height: 100%;",
         tabPosition: tabContainer.tabPosition,
         tabStrip:"true"
      });
      var tabs = (tabContainer.tabs==null)?[]:tabContainer.tabs;
      for (var index=0; index<tabs.length; index++){
         var tab = tabs[index];
         var selected = (tab.selected==null)?false:tab.selected;
         this.selectedIndex = 0;
         if (selected){
            this.selectedIndex = index;
            this.selectedData = {title:this.dataPage.title, form:this.dataPage.form};
         }
         var contentPane = new dijit.layout.ContentPane({
            title:tab.title,
            selected:selected
         });
         target.addChild(contentPane);
      }
      dojo.byId("insertContentId").appendChild(target.domNode);
      target.startup();

      var localWidgetId = this.widgetId;
      dojo.connect(target,"selectChild", function(child){
         var childIndex = target.getIndexOfChild(child);
         var eventName = localWidgetId + "_selectTabChild";
         console.log('Publish: ' + eventName);
         dojo.publish(eventName, [{childIndex:childIndex, widgetId:child.id}]);
      });
      this.childWidgetId = target.id;
   },
   _genericSimpleTab:function(tabContainer){
      this.selectedData = null;
      var htmlControl = '<div class="div_box_headerTab"><ul>';
      var tabs = (tabContainer.tabs==null)?[]:tabContainer.tabs;
      var tabIds = [];
      var tabObjects = [];
      for (var index=0; index<tabs.length; index++){
         var tab = tabs[index];
         var selected = (tab.selected==null)?false:tab.selected;
         this.selectedIndex = 0;
         if (selected){
            this.selectedIndex = index;
            this.selectedData = {title:this.dataPage.title, form:this.dataPage.form};
         }
         var tabId = this.widgetId+"_"+tab.id;
         tabIds[tabIds.length] = tabId;
         tabObjects[tabId] = {childIndex: index, widgetId:"viewContentQueueFragment"};
         htmlControl += '<li class="div_box_Tab"><a id="'+tabId+'" href="#"><span>'+tab.title+'</span></a>';
      }
      htmlControl += '</ul></div><div id="viewContentQueueFragment"></div>';
      dojo.byId(this.contentId).innerHTML = htmlControl;
      var localWidgetId = this.widgetId;
      for (var indexTab=0; indexTab<tabIds.length; indexTab++){
         dojo.query('#'+this.contentId+' [id="'+tabIds[indexTab]+'"]').onclick(function(){
            var eventName = localWidgetId + "_selectTabChild";
            console.log('Publish: ' + eventName);
            dojo.publish(eventName, [tabObjects[this.id]]);
         });
      }
      this.childWidgetId = "viewContentQueueFragment";
   },
   _genericPanel:function(dataLayout, layoutIndex){
      var dataContents = dataLayout.content;
      var htmlContent = "";
      var buttons = [];
      var elementPanel;
      if (layoutIndex == 0){
         elementPanel = dojo.byId(this.contentId);
      }else {
         var paneContentId = this.widgetId + "_genericPagePane";
         var childDiv = dojo.byId(paneContentId);
         if (childDiv == null){
            childDiv = dojo.doc.createElement("div");
            dojo.attr(childDiv, "id", paneContentId);
            dojo.byId(this.contentId).appendChild(childDiv);
         }
         elementPanel = childDiv;
      }
      elementPanel.innerHTML='';
      for (var index=0; index<dataContents.length; index++){
         var dataContent = dataContents[index];
         var contentType = dataContent.type;
         switch (contentType){
         case "button":
            var controlName = dataContent.name;
            var controlLabel = dataContent.label;
            var styleDisable = dataContent.disable=='true'?'disabled=true':'';
            var styleClass = dataContent.disable=='true'?'class="buttonDisableURL"':'class="buttonURL"';
            htmlContent = '<div class="divButtonURL"><div '+styleClass+'><a href="#" id="'+controlName+'" '+styleDisable+'>'+controlLabel+'</a></div></div>';
            elementPanel.innerHTML = htmlContent;
            buttons[buttons.length] = controlName;
            break;
         case "form":
            var divId = "div_form_genericPagePane_"+index;
            dojo.create("div",{"id":divId,"style":"weight:100%"},elementPanel,"last");
            var dataForm = this.dataPage[dataContent.name];
            var formData = {
               dataform: dataForm,
               contentId:divId,
               widgetId:divId+"_"+this.currentFormWidgetId
            };
            var genericForm = OPM.genericFormWidget(formData);
            genericForm.show();
            this.existedforms[this.existedforms.length] = genericForm;
            break;
         }
      }
      var localwidgetId = this.widgetId;
      for (var index=0; index<buttons.length; index++){
         dojo.query('#'+this.contentId+' [id="'+buttons[index]+'"]').onclick(function(){
            var eventName = localwidgetId+"_"+this.id+'Click';
            console.log('Publish: ' + eventName);
            dojo.publish(eventName, [this]);
         });
      }
   },
   _genericToolbar:function(dataLayout, layoutIndex){
      var content = dataLayout.content;
      var toolbarHTML = '<div id="_toolbarMenu" style="width: 100%"><div dojoType="dijit.Toolbar">';
      for (var index=0; index<content.length; index++){
        var object = content[index];
        toolbarHTML += '<div dojoType="dijit.form.Button" iconClass="'+object.iconClass+'" showLabel="false" onclick="connectEvent(\''+this.widgetId+'\',this, \''+object.name+'Click\')">';
        toolbarHTML += object.label;
        toolbarHTML += '</div>'
      }
      toolbarHTML += '</div></div>';
      dojo.html.set(dojo.byId(this.contentId), toolbarHTML, {parseContent: true});
   }
}
);
}